package com.huika.renrench.control.my.pay.llpay;

import org.json.JSONObject;








import com.huika.lib.ui.widget.ToastMsg;
import com.huika.renrench.control.my.activity.RechargeSuActivity;
import com.huika.renrench.control.my.merchant.SubmitSuccessActivity;
import com.huika.renrench.support.bean.LianLianBean;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class LianlPay {

	private Activity activity;
	private String realName;
	
	public LianlPay(Activity activity, String realName) {
		this.activity = activity;
		this.realName = realName;
	}
	
	/*
     * 支付验证方式 0：标准版本， 1：卡前置方式，此两种支付方式接入时，只需要配置一种即可，Demo为说明用。可以在menu中选择支付方式。
     */
    private int pay_type_flag = 0;
	
	private Handler yintongHandler = createHandler();

    private Handler createHandler() {
        return new Handler() {
            public void handleMessage(Message msg) {
                String strRet = (String) msg.obj;
                switch (msg.what) {
                    case Constants.RQF_PAY: {
                        JSONObject objContent = BaseHelper.string2JSON(strRet);
                        String retCode = objContent.optString("ret_code");
                        String retMsg = objContent.optString("ret_msg");
                        // TODO 先判断状态码，状态码为 成功或处理中 的需要 验签
                        if (Constants.RET_CODE_SUCCESS.equals(retCode)) {
                            // TODO 卡前置模式返回的银行卡绑定协议号，用来下次支付时使用，此处仅作为示例使用。正式接入时去掉
                            if (pay_type_flag == 1) {
//                                TextView tv_agree_no = (TextView) findViewById(R.id.tv_agree_no);
//                                tv_agree_no.setVisibility(View.VISIBLE);
//                                tv_agree_no.setText(objContent.optString(
//                                        "agreementno", ""));

                            }

                            String resulPay = objContent.optString("result_pay");
                            if (Constants.RESULT_PAY_SUCCESS
                                    .equalsIgnoreCase(resulPay)) {
//                                BaseHelper.showDialog(activity, "提示",
//                                        "支付成功，交易状态码：" + retCode,
//                                        android.R.drawable.ic_dialog_alert);
//                                ToastMsg.showToastMsg(activity, "支付成功");
                            	String orderTime = objContent.optString("dt_order");
                            	String orderNo = objContent.optString("oid_paybill");
                            	double amount = objContent.optDouble("money_order");
                                Intent intent = new Intent(activity, RechargeSuActivity.class);
                                intent.putExtra("orderTime", orderTime);
                                intent.putExtra("orderNo", orderNo);
                                intent.putExtra("amount", amount);
                                activity.startActivity(intent);
                                activity.finish();
                            } else {
                                BaseHelper.showDialog(activity, "提示",
                                        retMsg + "，交易状态码:" + retCode,
                                        android.R.drawable.ic_dialog_alert);
                            }

                        } else if (Constants.RET_CODE_PROCESS.equals(retCode)) {
                            String resulPay = objContent.optString("result_pay");
                            if (Constants.RESULT_PAY_PROCESSING
                                    .equalsIgnoreCase(resulPay)) {
                                BaseHelper.showDialog(activity, "提示",
                                        objContent.optString("ret_msg") + "交易状态码："
                                                + retCode,
                                        android.R.drawable.ic_dialog_alert);
                            }

                        } else {
                            BaseHelper.showDialog(activity, "提示", retMsg
                                    + "，交易状态码:" + retCode,
                                    android.R.drawable.ic_dialog_alert);
                        }
                    }
                        break;
                }
                super.handleMessage(msg);
            }
        };
    }
    
    private String getConten4Pay(LianLianBean bean) {
    	PayOrder order = new PayOrder();
    	order.setBusi_partner(bean.busi_partner);
        order.setNo_order(bean.no_order);
        order.setDt_order(bean.dt_order);
        order.setName_goods(bean.name_goods);
        order.setNotify_url(bean.notify_url);
        // MD5 签名方式
        order.setSign_type(bean.sign_type);
        // RSA 签名方式
        // order.setSign_type(PayOrder.SIGN_TYPE_RSA);

//        order.setValid_order("100");

        order.setUser_id(bean.user_id);
        order.setId_no(bean.id_no);

        order.setAcct_name(bean.acct_name);
//        order.setAcct_name(realName);
        order.setMoney_order(bean.money_order);

        // 银行卡卡号，该卡首次支付时必填
        order.setCard_no(bean.card_no);
        // 银行卡历次支付时填写，可以查询得到，协议号匹配会进入SDK，
//        order.setNo_agree(((EditText) findViewById(R.id.agree_no)).getText()
//                .toString());
        // 风险控制参数。
        order.setRisk_item(bean.risk_item);

        String sign = "";
        order.setOid_partner(bean.oid_partner);
        String content = BaseHelper.sortParam(order);
        // MD5 签名方式
        sign = Md5Algorithm.getInstance().sign(content, bean.md5_key);
        // RSA 签名方式
        // sign = Rsa.sign(content, EnvConstants.RSA_PRIVATE);
        order.setSign(sign);
    	return BaseHelper.toJSONString(order);
    }
    
    public void pay(LianLianBean bean) {
		MobileSecurePayer msp = new MobileSecurePayer();
		String content4Pay = getConten4Pay(bean);
		System.out.println("content4Pay:" + content4Pay);
//		content4Pay = "{\"oid_partner\":\"201408071000001546\",\"no_order\":\"20150123145648\",\"busi_partner\":\"101001\",\"sign_type\":\"MD5\",\"notify_url\":\"http:\\/\\/beta.www.youtx.com\\/youtxpay\\/LianlianPay\\/YTPay_Notify.aspx\",\"name_goods\":\"龙禧大酒店中餐厅：2-3人浪漫套餐X1\",\"acct_name\":\"\",\"money_order\":\"0.01\",\"sign\":\"b46785777bd28dff2d17b8032b5c573e\",\"risk_item\":\"{\\\"user_info_bind_phone\\\":\\\"13958069593\\\",\\\"frms_ware_category\\\":\\\"4.0\\\",\\\"user_info_dt_register\\\":\\\"201407251110120\\\",\\\"request_imei\\\":\\\"1122111221\\\"}\",\"valid_order\":\"100\",\"user_id\":\"\",\"dt_order\":\"20150123145648\",\"flag_modify\":\"0\",\"id_no\":\"\"}";
        boolean bRet = msp.pay(content4Pay, yintongHandler,
                Constants.RQF_PAY, activity, false);
	}
}
